package com.fishery.base.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.base.entity.Supply;
import com.fishery.base.service.SupplyService;
import com.fishery.entity.GlobalException;
import com.fishery.entity.Result;
import com.fishery.entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 投入品前端控制器
 * </p>
 *
 * @author Ghost
 * @since 2021-01-15
 */
@RestController
@RequestMapping("/supply")
@Api(tags = "投入品管理模块（偏向商家）")
@CrossOrigin
public class SupplyController {

	@Autowired
	private SupplyService supplyService;


	@PostMapping
	@ApiOperation("添加投入品")
	public Result addSupply(@RequestBody @ApiParam(value = "投入品对象") Supply supply) {
		if (supply == null
				|| StringUtils.isEmpty(supply.getName())
				|| StringUtils.isEmpty(supply.getSupplierName())
				|| StringUtils.isEmpty(supply.getInspector())) {
			return Result.error("添加失败");
		}
		if (supply.getType() <= 0 || supply.getType() >= 3) {
			throw new GlobalException(StatusCode.ERROR, "类型不符合");
		}
		boolean ret = supplyService.save(supply);
		// 保存类型
		return ret ? Result.success("添加成功") : Result.error("添加失败");
	}

	@GetMapping("/{size}/{current}")
	@ApiOperation("查询所有投入品（带分页）")
	public Result pageSupply(@PathVariable(name = "size", required = true) @ApiParam(value = "尺寸", required = true) int size,
							 @PathVariable(name = "current", required = true) @ApiParam(value = "页码", required = true) int current) {
		Page<Supply> page = supplyService.getSupplyWithPage(size, current);
		return Result.success().data(page);
	}

	@GetMapping
	@ApiOperation("查询所有投入品的")
	public Result getAllSupplies() {
		return Result.success().data(supplyService.list(null));
	}

	@DeleteMapping("/{supplyId}")
	@ApiOperation("通过投入品ID删除投入品")
	public Result deleteSupply(@PathVariable(name = "supplyId", required = true) @ApiParam(value = "投入品ID", required = true) String supplyId) {
		if (StringUtils.isEmpty(supplyId)) {
			return Result.error("删除失败");
		}
		boolean ret = supplyService.removeById(supplyId);
		return ret ? Result.success("删除成功") : Result.error("删除失败");
	}

	@GetMapping("/{id}")
	@ApiOperation("查询投入品信息")
	public Result getSupply(@PathVariable(name = "id", required = true) @ApiParam(value = "id", required = true) String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error("查询失败");
		}
		return Result.success().data(supplyService.getById(id));
	}

	@PutMapping
	@ApiOperation("修改投入品信息")
	public Result updateSupply(@RequestBody @ApiParam(value = "投入品对象") Supply supply) {
		if (supply == null || StringUtils.isEmpty(supply.getId())) {
			return Result.error("修改失败");
		}
		if (supply.getType() <= 0 || supply.getType() >= 3) {
			throw new GlobalException(StatusCode.ERROR, "类型不符合");
		}
		boolean ret = supplyService.updateById(supply);
		return ret ? Result.success("修改成功") : Result.error("修改失败");
	}


	@GetMapping("/sort/{type}")
	@ApiOperation("查询某一种类型的所有投入品")
	public Result getOneTypeSupplies(@PathVariable(name = "type", required = true)
									 @ApiParam(value = "类型值（1、渔药,2、饲料）") Integer type) {
		if (type == null) {
			return Result.error("查询失败");
		}
		List<Supply> list = supplyService.getAllSuppliesByType(type);
		return Result.success().data(list);
	}

}

